
-----创建一个按钮(子图,点击缩放1.2
GFunc_CreateButton = function(sfile, _width, _height, index, func)
    local btnSpriteN = CCSprite:create(sfile, CCRectMake(_width*index, 0, _width, _height))
    local btnSpriteS = CCSprite:create(sfile, CCRectMake(_width*index, 0, _width, _height))
    btnSpriteS:setScale(1.2)
    btnSpriteS:setContentSize(CCSizeMake(_width*1.2, _height*1.2))
    local btnItem = CCMenuItemSprite:create(btnSpriteN, btnSpriteS)
    if func ~= nil then
        btnItem:registerScriptTapHandler(func)
    end
    
    return btnItem
end

-----创建一个按钮-plist图片
GFunc_CreateButtonP = function(sfile, func, scale, scaleT)
    local btnSpriteN = CCSprite:createWithSpriteFrameName(sfile)
    local btnSpriteS = CCSprite:createWithSpriteFrameName(sfile)
    
    if scale == nil then
        scale = 1.1
    end
    
    btnSpriteS:setScale(scale)
    btnSpriteS:setContentSize(CCSizeMake(btnSpriteN:getContentSize().width * scale, btnSpriteN:getContentSize().height * scale))
    local btnItem = CCMenuItemSprite:create(btnSpriteN, btnSpriteS)
    if func ~= nil then
        btnItem:registerScriptTapHandler(func)
    end
    
    --可触摸区域大小
    if scaleT then
        btnItem:setContentSize(CCSizeMake(btnSpriteN:getContentSize().width * scaleT, btnSpriteN:getContentSize().height * scaleT))
    end
    
    return btnItem
end

GFunc_CreateButtonDP = function(sfile, func, scale)
    local btnItem = GFunc_CreateButtonP(sfile, func, scale)
    local btnSpriteD = CCSprite:createWithSpriteFrameName(sfile)
    btnSpriteD:setColor(ccc3(170, 96, 59))
    btnItem:setDisabledImage(btnSpriteD)
    return btnItem
end

GFunc_CreateAnimation = function(fturext, bframe, iframe, loop, fps, func)
    local animation = CCAnimation:create()
    local name = nil
    for i = bframe, iframe do
        number = i
        name = fturext..number..".png"
        local pFrame = CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(name);
        animation:addSpriteFrame(pFrame);
    end
    --频率
    if fps == nil then
        fps = 0.1
    end
    if loop == nil then
        loop = 10000
    end
    
    animation:setDelayPerUnit(fps)
    animation:setRestoreOriginalFrame(true)
    animation:setLoops(loop)--循环次数
    local array = CCArray:create()
    array:addObject(CCAnimate:create(animation))
    local seq = CCSequence:create(array)
    
    if func ~= nil then
        seq = CCSequence:createWithTwoActions(seq, CCCallFuncN:create(func))
    end
    
    local anim = CCSprite:createWithSpriteFrameName(name)
    anim:runAction(seq)
    
    return anim
end

---暴走物资专用
local gbaozouindex = 0
local gbaozou = nil
local gbaozouTag = 1999999

Gfunc_DelBazouEffs = function()
    for i=0, gbaozouindex-1 do
        CCDirector:sharedDirector():getRunningScene():removeChildByTag(gbaozouTag + i, true)
    end
    gbaozou = nil
end

GFunc_ShowBaozouEffs = function()
    if gbaozou ~= nil then
        Gfunc_DelBazouEffs()
    end
    
    local h = 239--240
    local w = 159--160
    local hl = math.ceil(DWinSizeHeight / h)
    local wl = math.ceil(DWinSizeWidth / h)
    gbaozouindex = (hl + wl) * 2
    
    --左
    local tag = gbaozouTag
    for i=0, hl-1 do
        gbaozou = GFunc_CreateAnimation("mad_0000", 0, 4)
        gbaozou:setPosition(gdccp(0, i * h))
        gbaozou:setAnchorPoint(ccp(0, 0))
        CCDirector:sharedDirector():getRunningScene():addChild(gbaozou, 0, tag)
        tag = tag + 1
    end
    --下
    for i=0,wl-1 do
        gbaozou = GFunc_CreateAnimation("mad_0000", 0, 4)
        gbaozou:setPosition(gdccp(i * h,0))
        gbaozou:setRotation(90)
        gbaozou:setAnchorPoint(ccp(1, 0))
        gbaozou:setFlipX(true)
        CCDirector:sharedDirector():getRunningScene():addChild(gbaozou, 0, tag)
        tag = tag + 1
    end
    --右
    for i=0, hl-1 do
        gbaozou = GFunc_CreateAnimation("mad_0000", 0, 4)
        gbaozou:setPosition(gdccp(DWinSizeWidth, i * h))
        gbaozou:setAnchorPoint(ccp(1, 0))
        gbaozou:setFlipX(true)
        CCDirector:sharedDirector():getRunningScene():addChild(gbaozou, 0, tag)
        tag = tag + 1
    end
    --上
    for i=0,wl-1 do
        gbaozou = GFunc_CreateAnimation("mad_0000", 0, 4)
        gbaozou:setPosition(gdccp(i * h, DWinSizeHeight))
        gbaozou:setRotation(90)
        gbaozou:setAnchorPoint(ccp(0, 0))
        CCDirector:sharedDirector():getRunningScene():addChild(gbaozou, 0, tag)
        tag = tag + 1
    end
end

---无知效果
local gWudiTag = 1999899
GFunc_HideWudiEffs = function()
    CCDirector:sharedDirector():getRunningScene():removeChildByTag(gWudiTag, true)
end
GFunc_ShowWudiEffs = function()
    local anim = GFunc_CreateAnimation("effwudi_0", 1, 6)
    anim:setPosition(ccp(PlayerBegPosX, RoleBegPosY-gdv(10)))
    anim:setAnchorPoint(ccp(0.5, 0))
    CCDirector:sharedDirector():getRunningScene():addChild(anim, 0, gWudiTag)
end

--------连击效果
local combolayer = nil
GFunc_ComboEffs = function(combo, pos)
    ---音效
    local effFile = sme_Combo1
    if combo == 1 then
        effFile = sme_Combo1
    elseif combo == 2 then
        effFile = sme_Combo2
    elseif combo == 3 then
        effFile = sme_Combo3
    elseif combo == 4 then
        effFile = sme_Combo4
    elseif combo == 5 then
        effFile = sme_Combo5
    elseif combo == 6 then
        effFile = sme_Combo6
    elseif combo == 7 then
        effFile = sme_Combo7
    else
        effFile = sme_Combo8
    end
    
    Play_Effect(effFile)
    
    
    if combo < 2 then
        return
    end
    
    local function Func_deleteMy()
         CCDirector:sharedDirector():getRunningScene():removeChild(combolayer, true)
         combolayer = nil
    end
    
    if combolayer ~= nil then
        Func_deleteMy()
    end
    
    combolayer = CCLayer:create()
    
    local parent = CCNode:create()
    combolayer:addChild(parent)
    
    local _bgBrick = CCSprite:createWithSpriteFrameName("ComboImg.png")
    _bgBrick:setAnchorPoint(ccp(0, 0.5))
    parent:addChild(_bgBrick)
    
    
    local cnumber = GameNumbers:new(vNumberCombo, NumberAlginment_Right)
    cnumber:setNumber(combo)
    cnumber:setPosition(gdccp(0,15))
    cnumber:addLayer(parent)
    
    --combo = 10
    --parent:setScale(0.2+combo/30.0)
    parent:setScale(0.7)
    local array = CCArray:create()
    --array:addObject(CCScaleTo:create(0.2, 0.6+combo/25.0))
    array:addObject(CCScaleTo:create(0.2, 1.0))
    array:addObject(CCFadeTo:create(1.0, 255))
    array:addObject(CCFadeTo:create(1.0, 0))
    
    local seq = CCSequence:create(array)
    --parent:runAction(CCSequence:create(array))
    parent:runAction(CCSequence:createWithTwoActions(seq, CCCallFuncN:create(Func_deleteMy)))
    
    
    --parent:setPosition(gdccp(pos.x, pos.y))
    local px, py = getGemPos(0,0)
    if combo >= 10 then
        px = px + gdv(30)
    end
    parent:setPosition(ccp(px, py))
    
    CCDirector:sharedDirector():getRunningScene():addChild(combolayer)
end




---------------可前后翻页的layer窗口
GameContainerPage = {}
--这句是重定义元表的索引，必须要有，
GameContainerPage.__index = GameContainerPage
--模拟构造体，一般名称为new()大小,
function GameContainerPage:new(w, h)
    local self = {}   
    setmetatable(self, GameContainerPage)
    
    self._width = w
    self._height = h
    
    self._childs = {}
    -----
    
    self._parnet = CCLayer:create()
    
    self._showIndex = 0
    
    --上次操作时间(操作间隔
    local _lastTimes = os.clock()
    local _mousex, _mousey = 0 ,0
    local _beginpx = 0
    local function onTouchBegin(x, y)
        _mousex, _mousey = x, y
        _beginpx = x
        
        return 1
    end
    
    local function onTouchMoved(x, y)
    end
    
    local function onTouchEnded(x, y)
        if _lastTimes + 0.31 > os.clock() then
            return
        end
        _lastTimes = os.clock()
        
        -- 切换显示的列表
        local badd = nil
        if x > _beginpx + 20 then
            badd = false
        elseif x < _beginpx - 20 then
            badd = true
        end
        if badd ~= nil then
            self:Show_Layer(badd)
        end
    end
    
    -- 触摸操作
    local function onTouch(eventType, x, y)
        if eventType == CCTOUCHBEGAN then
            return onTouchBegin(x, y)
        elseif eventType == CCTOUCHMOVED then
            return onTouchMoved(x, y)
        elseif eventType == CCTOUCHENDED then
            return onTouchEnded(x, y)
        end
    end
    
    self._parnet:setTouchEnabled(true)
    self._parnet:registerScriptTouchHandler(onTouch)
    
    return self
end

function GameContainerPage:addToLayer(layer, rlevel)
    layer:addChild(self._parnet)
end

function GameContainerPage:addChild(child)
    table.insert(self._childs, child)
    self._parnet:addChild(child)
    
    ---初始化位置
    self:Show_Layer(nil, true)
end

function GameContainerPage:setPosition(pos)
    self._parnet:setPosition(pos)
end

function GameContainerPage:Show_Layer(badd, binit)
    if badd == true then
        self._showIndex = self._showIndex + 1
    elseif badd == false then
        self._showIndex = self._showIndex - 1
    end
    
    if self._showIndex >= #self._childs then
        self._showIndex = #self._childs - self._showIndex
    elseif self._showIndex < 0 then
        self._showIndex = #self._childs + self._showIndex
    end
    
    table.foreach(self._childs, function(k,ly)
        local midx = k - 1 - self._showIndex
        
        local imd = math.floor(#self._childs / 2)
        if midx < -imd then
            midx = #self._childs + midx
        elseif midx > imd then
            midx = -(#self._childs - midx)
        end
        
        if not binit and ((badd and (midx == -1 or midx == 0)) or (not badd and (midx == 0 or midx == 1))) then
            ly:runAction(CCMoveTo:create(0.3, ccp(midx * self._width, 0)))
            ly:setVisible(true)
        else
            ly:setPosition(gdccp(midx * self._width, 0))
            ly:setVisible(false)
            
            if binit and midx == 0 then
                ly:setVisible(true)
            end
        end
    end)
end


--------- Layer创建 创建一个layer,响应刷新/进入/离开/触摸/触摸移动/触摸离开/事件
--local func_updateLayer = function(dt) end
--local func_enterLayer = function() end
--local func_exitLayer = function() end
--local func_touchBegin = function(x,y) end
--local func_touchMoved = function(x,y) end
--local func_touchEnded = function(x,y) end
--fps=1.0/60.0
--fmbg = false
--color=nil
--layer = GFunc_CreateLayerEnterOrExit(color,
--    func_updateLayer,func_enterLayer,func_exitLayer,func_touchBegin,func_touchMoved,func_touchEnded,
--    fps,fmbg)
GFunc_CreateLayerEnterOrExit = function(color, upfunc, enfunc, exfunc, tbfunc, tmfunc, tefunc, fps, fmbg)
    if color == nil then
        color = ccc4(0,0,0,0)
    end
    
    local layer = CCLayerColor:create(color)
    
    if fps == nil then
        fps = 1.0/60.0
    end
    
    if fmbg ~= nil and fmbg == true then
        -- 背景//遮挡按钮用
        local _Menubg = CCMenu:create()
        -- 主菜单大小
        _Menubg:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
        -- 设置坐标
        _Menubg:setPosition(0, 0)
        layer:addChild(_Menubg)
        
        local function menucallback()
            if tbfunc ~= nil then
                tbfunc()
            end
        end

        local btnItem = nil
        btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial", FontSize))
        btnItem:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
        btnItem:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
        btnItem:registerScriptTapHandler(menucallback)
        _Menubg:addChild(btnItem)
    end
    
    --更新事件回调
    local function update(dt)
        if upfunc ~= nil then
            upfunc(dt)
        end
    end
    
    ---进入退出界面
    local _joinGame = false
    local _baseLayer_entry = nil
    local function SpriteEase_onEnterOrExit(tag,tag2)
        if not _joinGame then
            _joinGame = true
            
            if enfunc ~= nil then
                enfunc()
            end
            
            if upfunc ~= nil then
                _baseLayer_entry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, fps,false)
            end
        else
            _joinGame = false
            
            if exfunc ~= nil then
                exfunc()
            end
            
            if _baseLayer_entry ~= nil then
                CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(_baseLayer_entry)
            end
            _baseLayer_entry = nil
        end
    end
    -- 注册进入/退出事件
    layer:registerScriptHandler(SpriteEase_onEnterOrExit)
    
    -------------触摸事件
    local function onTouchBegin(x, y)
        if tbfunc ~= nil then
            tbfunc(x,y)
        end
        
        if tmfunc ~= nil then
            return 1
        else
            return 0
        end
    end
    
    local function onTouchMoved(x, y)
        if tmfunc ~= nil then
            tmfunc(x,y)
        end
    end
    
    local function onTouchEnded(x, y)
        if tefunc ~= nil then
            tefunc(x, y)
        end
    end
    
    -- 触摸操作
    local function onTouch(eventType, x, y)
        if eventType == CCTOUCHBEGAN then
            return onTouchBegin(x, y)
        elseif eventType == CCTOUCHMOVED then
            return onTouchMoved(x, y)
        elseif eventType == CCTOUCHENDED then
            return onTouchEnded(x, y)
        end
    end
    
    if tmfunc ~= nil or tbfunc ~= nil or tefunc ~= nil then
        layer:setTouchEnabled(true)
        layer:registerScriptTouchHandler(onTouch)
    end
    
    return layer
end


------------ 像短信提示一样,显示一条信息,从上方进入屏幕,再离开屏幕
--信息列表
local SMessageTable = {}
local SMShow = false
local SMLayer = nil
local SMTxtMsg = nil
local SMReturn = false

GFunc_ClearSMessage = function()
    SMessageTable = {}
    CCDirector:sharedDirector():getRunningScene():removeChild(SMLayer, true)
    SMLayer = nil
    SMShow = false
    SMReturn = false
    --print("clear")
end

GFunc_ShowSMessage = function(msg)
    if msg ~= nil then
        table.insert(SMessageTable, msg)
    end
    --print("11", SMLayer)
    local msgSize = 30
    local showX = 0
    local showY = DWinSizeHeight
    local showToY = showY-(msgSize+10)
    if SMLayer == nil then
        SMLayer = CCLayerColor:create(ccc4(0,0,0,100))
        SMLayer:setPosition(gdccp(showX, showY))
        
        SMTxtMsg = CCLabelTTF:create(SMessageTable[1], "Arial", FontSize6)
        SMTxtMsg:setPosition(gdccp(DWinSizeWidth/2, msgSize/2+5))
        SMLayer:addChild(SMTxtMsg)
        
        CCDirector:sharedDirector():getRunningScene():addChild(SMLayer,10)
    end
    
    if SMReturn then
        SMTxtMsg:setString(SMessageTable[1])
    end
    
    local func_nextMsg = function()
        -- 如果还有下一条信息,则显示下一条信息
        table.remove(SMessageTable, 1)
        if #SMessageTable > 0 then
            --渐隐,切换文字,再显示
            SMTxtMsg:runAction(CCSequence:createWithTwoActions(CCFadeTo:create(0.5, 10), CCCallFunc:create(function()
                SMTxtMsg:setString(SMessageTable[1])
                SMTxtMsg:runAction(CCSequence:createWithTwoActions(CCFadeTo:create(0.5, 255), CCCallFunc:create(function()
                    GFunc_ShowSMessage()
                    SMReturn = false
                end)))
            end)))
            
        else
            local action = CCMoveTo:create(0.5, gdccp(showX, showY))
            local sqe = CCSequence:createWithTwoActions(action, CCCallFunc:create(function()
                GFunc_ClearSMessage()
            end))
            SMLayer:runAction(sqe)
        end
        SMReturn = true
    end
    
    if not SMShow or SMReturn then
        SMLayer:stopAllActions()
        
        local x, y = SMLayer:getPosition()
        
        local array = CCArray:create()
        array:addObject(CCMoveTo:create((y - gdv(showToY)) * 0.0125, gdccp(showX, showToY)))
        array:addObject(CCMoveTo:create(3, gdccp(showX, showToY)))
        --array:addObject(CCMoveTo:create(0.5, ccp(showX, showY)))
        local sqe = CCSequence:create(array)
        SMLayer:runAction(CCSequence:createWithTwoActions(sqe, CCCallFunc:create(func_nextMsg)))
    end
    
    SMShow = true
    CCDirector:sharedDirector():getRunningScene():setTag(11)
end







--------- Layer创建 创建一个layer,响应刷新/进入/离开/触摸/触摸移动/触摸离开/事件
--local func_updateLayer = function(dt) end
--local func_enterLayer = function() end
--local func_exitLayer = function() end
--local func_touchBegin = function(x,y) end
--local func_touchMoved = function(x,y) end
--local func_touchEnded = function(x,y) end
--fps=1.0/60.0
--fmbg = false
--color=nil
--layer = GFunc_CreateLayerEnterOrExit(color,
--    func_updateLayer,func_enterLayer,func_exitLayer,func_touchBegin,func_touchMoved,func_touchEnded,
--    fps,fmbg)
GFunc_CreateLayerEnterOrExit = function(color, upfunc, enfunc, exfunc, tbfunc, tmfunc, tefunc, fps, fmbg)
    if color == nil then
        color = ccc4(0,0,0,0)
    end
    
    local layer = CCLayerColor:create(color)
    
    if fps == nil then
        fps = 1.0/60.0
    end
    
    if fmbg ~= nil and fmbg == true then
        -- 背景//遮挡按钮用
        local _Menubg = CCMenu:create()
        -- 主菜单大小
        _Menubg:setContentSize(CCSizeMake(DWinSize.width,  DWinSize.height))
        -- 设置坐标
        _Menubg:setPosition(0, 0)
        layer:addChild(_Menubg)
        
        local function menucallback()
            if tbfunc ~= nil then
                tbfunc()
            end
        end

        local btnItem = nil
        btnItem = CCMenuItemLabel:create(CCLabelTTF:create("", "Arial", GFGetFont(FontSize)))
        btnItem:setPosition(ccp(DWinSize.width/2, DWinSize.height/2))
        btnItem:setContentSize(CCSizeMake(DWinSize.width,  DWinSize.height))
        btnItem:registerScriptTapHandler(menucallback)
        _Menubg:addChild(btnItem)
    end
    
    --更新事件回调
    local function update(dt)
        if upfunc ~= nil then
            upfunc(dt)
        end
    end
    
    ---进入退出界面
    local _joinGame = false
    local _baseLayer_entry = nil
    local function SpriteEase_onEnterOrExit(tag,tag2)
        if not _joinGame then
            _joinGame = true
            
            if enfunc then
                enfunc()
            end
            
            if upfunc then
                _baseLayer_entry = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, fps,false)
            end
        else
            _joinGame = false
            
            if exfunc then
                exfunc()
            end
            
            if _baseLayer_entry ~= nil then
                CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(_baseLayer_entry)
            end
            _baseLayer_entry = nil
        end
    end
    -- 注册进入/退出事件
    layer:registerScriptHandler(SpriteEase_onEnterOrExit)
    
    -------------触摸事件
    local function onTouchBegin(x, y)
        if tbfunc ~= nil then
            tbfunc(x,y)
        end
        
        if tmfunc ~= nil then
            return 1
        else
            return 0
        end
    end
    
    local function onTouchMoved(x, y)
        if tmfunc ~= nil then
            tmfunc(x,y)
        end
    end
    
    local function onTouchEnded(x, y)
        if tefunc ~= nil then
            tefunc(x, y)
        end
    end
    
    -- 触摸操作
    local function onTouch(eventType, x, y)
        if eventType == CCTOUCHBEGAN then
            return onTouchBegin(x, y)
        elseif eventType == CCTOUCHMOVED then
            return onTouchMoved(x, y)
        elseif eventType == CCTOUCHENDED then
            return onTouchEnded(x, y)
        end
    end
    
    if tmfunc ~= nil or tbfunc ~= nil or tefunc ~= nil then
        layer:setTouchEnabled(true)
        
        if fmbg and tmfunc and tbfunc then
            layer:registerScriptTouchHandler(onTouch, false, -129, true)
        else
            layer:registerScriptTouchHandler(onTouch)
        end
    end
    
    return layer
end



